using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Xeml.Document;

namespace Xid.Controls
{
    public partial class GermPlasmEditCtrl : UserControl, INotifyPropertyChanged
    {
        private AnnotationForm af;
        public string germPlasmId;

        private bool germPlasmAssigned = false;


        private string freeText;

        public string FreeText
        {
            get { return textBoxFreeAnnotation.Text; }
            set { textBoxFreeAnnotation.Text = value; }
        }


        private IndividualsPool pool;

        public IndividualsPool IndividualsPool
        {
            get { return pool; }
            set
            {
                pool = value;
                if(pool != null)
                    SetupGermplasmId(pool.GermPlasm);
            }
        }


        public string GermPlasmId
        {
            get { return germPlasmId; }
            set
            {
                if (germPlasmId != value)
                {
                    germPlasmId = value;
                    //germPlasmAssigned = true;
                    OnPropertyChanged("GermPlasmId");
                }
            }
        }

        private int poolSize;

        public int PoolSize
        {
            get { return poolSize; }
            set { poolSize = value; }
        }


        private Dictionary<string, string> binomialNames = new Dictionary<string, string>();
        private List<string> accessions = new List<string>();
        private List<string> mutants = new List<string>();
        private List<string> transgenics = new List<string>();

        public void SetupGermplasmId(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                string[] x = id.Split(".".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                if (x.Length < 1)
                {
                    errorProvider.SetError(comboBoxBinomialName, "At least the species has to be defined.");

                }
                else
                {
                    if (binomialNames.ContainsValue(x[0]))
                        comboBoxBinomialName.SelectedValue = x[0];
                    else
                        comboBoxBinomialName.Text = x[0];

                    if (x.Length >= 3)
                    {
                        //if (accessions.Contains(x[1]))
                        //    comboBoxAccession.SelectedValue = x[1];
                        //else
                        //{
                        comboBoxAccession.Text = x[1].Replace("_", ""); ;
                        //}
                    }
                    if (x.Length >= 3)
                    {
                        //if (mutants.Contains(x[2]))
                        //    comboBoxMutant.SelectedValue = x[2];
                        //else
                        //{
                        comboBoxMutant.Text = x[2].Replace("_", ""); ;
                        //}
                    }
                    if (x.Length >= 4)
                    {
                        //if (transgenics.Contains(x[3]))
                        //    comboBoxTransgenic.SelectedValue = x[3];
                        //else
                        //{
                        comboBoxTransgenic.Text = x[3].Replace("_", ""); ;
                        //}
                    }
                }
            }
        }


        public GermPlasmEditCtrl()
        {
           
            InitializeComponent();
            this.Validating += new CancelEventHandler(GermPlasmEditCtrl_Validating);
            binomialNames.Add("Arabidopsis thaliana", "At");
          //  binomialNames.Add("Arabidopsis halleri", "Ah");
            accessions.Add("Col-0");
            mutants.Add("unknown");
            mutants.Add("pgm");
            mutants.Add("pgm'1");
            mutants.Add("pgm'2");
            mutants.Add("pgm'3");

            transgenics.Add("unknown");
            //transgenics.Add("42");
            //transgenics.Add("0815");


            this.comboBoxBinomialName.DataSource = new BindingSource(binomialNames, null);
            this.comboBoxBinomialName.DisplayMember = "Key";
            this.comboBoxBinomialName.ValueMember = "Value";

            this.comboBoxAccession.DataSource = new BindingSource(accessions, null);
            this.comboBoxMutant.DataSource = new BindingSource(mutants, null);
            this.comboBoxTransgenic.DataSource = new BindingSource(transgenics, null);

            comboBoxBinomialName.TextChanged += new EventHandler(comboBoxBinomialName_TextChanged);
            comboBoxAccession.TextChanged += new EventHandler(comboBoxAccession_TextChanged);
            comboBoxMutant.TextChanged += new EventHandler(comboBoxMutant_TextChanged);
            comboBoxTransgenic.TextChanged += new EventHandler(comboBoxTransgenic_TextChanged);
            comboBoxBinomialName.SelectedIndex = -1;
            comboBoxAccession.SelectedIndex = -1;
            comboBoxMutant.SelectedIndex = -1;
            comboBoxTransgenic.SelectedIndex = -1;


            this.textBoxGermPlasmId.DataBindings.Add("Text", this, "GermPlasmId", true, DataSourceUpdateMode.OnPropertyChanged);
            this.textBoxGermPlasmId.DataBindings["Text"].ControlUpdateMode = ControlUpdateMode.OnPropertyChanged;
            // this.numericUpDownPoolSize.DataBindings.Add("Value", this, "poolSize", false, DataSourceUpdateMode.OnPropertyChanged);
            this.textBoxFreeAnnotation.DataBindings.Add("Text", this, "FreeText");

            af = new AnnotationForm();
        }

        void GermPlasmEditCtrl_Validating(object sender, CancelEventArgs e)
        {

            if (string.IsNullOrEmpty(comboBoxBinomialName.Text))
            {
                errorProvider.SetError(comboBoxBinomialName, "At least the species has to be defined.");
                e.Cancel = true;
            }

            //throw new Exception("The method or operation is not implemented.");
        }

        void comboBoxTransgenic_TextChanged(object sender, EventArgs e)
        {
            BuildId();
        }

        void comboBoxMutant_TextChanged(object sender, EventArgs e)
        {
            BuildId();
        }

        void comboBoxAccession_TextChanged(object sender, EventArgs e)
        {
            BuildId();
        }

        void comboBoxBinomialName_TextChanged(object sender, EventArgs e)
        {
            BuildId();
        }

        private void GermPlasmEditCtrl_Load(object sender, EventArgs e)
        {

            //BuildId();

        }

        private void BuildId()
        {
            StringBuilder sb = new StringBuilder();



            if (binomialNames.ContainsKey(comboBoxBinomialName.Text))
                sb.Append(binomialNames[comboBoxBinomialName.Text]);
            else
                sb.Append(comboBoxBinomialName.Text);


            string subspecies = "";
            //if (accessions.Contains(comboBoxAccession.Text))
            //    subspecies = accessions.Find(delegate (string s){s ==  } [comboBoxBinomialName.Text];
            //else
            subspecies = comboBoxAccession.Text;

            string mutant = "";
            //if (mutants.Contains(comboBoxMutant.Text))
            //    mutant = mutants[comboBoxBinomialName.Text];
            //else
            mutant = comboBoxMutant.Text;

            string transgenic = "";
            //if (mutants.Contains(comboBoxMutant.Text))
            //    mutant = mutants[comboBoxBinomialName.Text];
            //else
            transgenic = comboBoxTransgenic.Text;

            if (!String.IsNullOrEmpty(subspecies))
            {
                if (subspecies == "unknown")
                    sb.AppendFormat(".{0}", "?");
                else
                    sb.AppendFormat(".{0}", subspecies);
            }

            if (!String.IsNullOrEmpty(mutant))
            {
                if (String.IsNullOrEmpty(subspecies))
                {
                    sb.AppendFormat(".{0}", "_");
                }
                if (mutant == "unknown")
                    sb.AppendFormat(".{0}", "?");
                else
                    sb.AppendFormat(".{0}", mutant);
            }

            if (!String.IsNullOrEmpty(transgenic))
            {
                if (String.IsNullOrEmpty(mutant))
                {
                    if (String.IsNullOrEmpty(subspecies))
                    {
                        sb.AppendFormat(".{0}", "_");
                    }

                    sb.AppendFormat(".{0}", "_");
                }
                if (transgenic == "unknown")
                    sb.AppendFormat(".{0}", "?");
                else
                    sb.AppendFormat(".{0}", transgenic);
            }


            textBoxGermPlasmId.Text = sb.ToString();
        }

        #region INotifyPropertyChanged Members

        protected virtual void OnPropertyChanged(string prop)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void pictureBoxAnnotatioGermPlasm_Click(object sender, EventArgs e)
        {
            af.Setup(this.pool, (sender as Control).Tag as string);
            af.ShowDialog(this);
        }

        
       
    }
}
